﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using NCT.Framework.Database;
using NCT.Framework;
using System.Data;

namespace VISS.API.Commons {
    class CityBC : ICity {

        #region ICity Members

        public bool Insert(CityEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Normalize data
                item.Name = StringUtils.Normalize(item.Name);

                //Create connection & transaction
                dbCon = Database.CreateConnection(ConfigInfo.ConnectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Add City to DB
                result = CityDA.Insert(item, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                if (result && ConfigInfo.UseCache) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.CityBC.Insert({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public bool Update(CityEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Normalize data
                item.Name = StringUtils.Normalize(item.Name);

                //Create connection & transaction
                dbCon = Database.CreateConnection(ConfigInfo.ConnectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Update City to DB
                result = CityDA.Update(item, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                if (result && ConfigInfo.UseCache) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.CityBC.Update({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public bool Delete(short iD) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {

                //Create connection & transaction
                dbCon = Database.CreateConnection(ConfigInfo.ConnectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Update City to DB
                result = CityDA.Delete(iD, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                if (result && ConfigInfo.UseCache) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.CityBC.Update({0}): \n" + ex.ToString(),
                                    iD));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public List<CityEnt> GetAll() {
            List<CityEnt> result = new List<CityEnt>();
            try {
                if (ConfigInfo.UseCache) {
                }
                else {
                    result = CityDA.GetAll(null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error("VISS.Commons.CityBC.GetAll(): \n" + ex.ToString());
            }

            return result;
        }

        #endregion
    }
}
